package com.draemoncode

/**
 * Created by zhou on 2017/4/22.
 */
class Script {
    Map settle(Map zhuang, List<Map> xianList, Map result){
        def xianAccountLimitUpEnable = true;
        BigDecimal zhuangAmount = 0;
        for(Map item : xianList){
            Object[] win = whowin(zhuang['num'], item.num);
            BigDecimal amount = item['zhu'].toBigDecimal() * win[1].toBigDecimal();
            if(win[0] == '0'){
                result.put(item.id, new BigDecimal(0));
            }else if(win[0] == 'zhuang'){
                def xianAccount = item['account'].toBigDecimal();
                def trueAmount = amount;
                if(xianAccount < amount){
                    trueAmount = xianAccount;
                }
                result.put(item.id, trueAmount * -1);
                zhuangAmount += trueAmount;
            }else if(win[0] == 'xian'){
                def zhuangLeft = zhuang['limit'].toBigDecimal() + zhuangAmount.toBigDecimal();
                def xianLimit = item['account'].toBigDecimal();
                def trueAmount = amount;
                if(xianAccountLimitUpEnable){
                    if(amount > xianLimit){
                        trueAmount = xianLimit;
                    }
                }
                if(trueAmount > zhuangLeft){
                    trueAmount = zhuangLeft;
                }
                result.put(item.id, trueAmount);
                zhuangAmount -= trueAmount;
            }
        }
        result.put(zhuang.id, zhuangAmount);
        return result;
    }

    Object[] whowin(zhuangNum, xianNum){
        def zhuangPoint = 0;
        def xianPoint = 0;
        def winPoint = zhuangPoint;
        def winner = 'zhuang';
        def winBei = 0;
        if(zhuangNum[0] == zhuangNum[2] && zhuangNum[2] == zhuangNum[3]){
            zhuangPoint = zhuangNum[0].toInteger() * 1000;
        }else if(zhuangNum[2] == zhuangNum[3]){
            if(zhuangNum[2].toInteger() == 0){
                zhuangPoint = zhuangNum[0].toInteger() * 100;
            }else{
                zhuangPoint = zhuangNum[2].toInteger() * 10;
            }
        }else{
            zhuangPoint = (zhuangNum[2].toInteger() + zhuangNum[3].toInteger()) % 10;
        }

        if(xianNum[0] == xianNum[2] && xianNum[2] == xianNum[3]){
            xianPoint = xianNum[0].toInteger() * 1000;
        }else if(xianNum[2] == xianNum[3]){
            if(zhuangNum[2].toInteger() == 0){
                xianPoint = xianNum[0].toInteger() * 100;
            }else{
                xianPoint = xianNum[2].toInteger() * 10;
            }
        }else{
            xianPoint = (xianNum[2].toInteger() + xianNum[3].toInteger()) % 10;
        }
        if(zhuangPoint == 0 && xianPoint == 0){
            winPoint = 1;
            winner = 'zhuang';
        }else if(zhuangPoint > xianPoint){
            winPoint = zhuangPoint;
            winner = 'zhuang';
        }else if(zhuangPoint == xianPoint){
            winPoint = 0;
            winner = '0';
        }else{
            winPoint = xianPoint;
            winner = 'xian';
        }
        if(winPoint >= 1000){
            winBei = 8;
        }else if(winPoint >= 100){
            winBei = 7;
        }else if(winPoint >= 10){
            winBei = 4;
        }else if(winPoint == 9){
            winBei = 3;
        }else if(winPoint == 8){
            winBei = 2;
        }else if(winPoint > 0 && winPoint < 8){
            winBei = 1;
        }else{
            winBei = 0;
        }
        def result = new Object[2];
        result[0] = winner;
        result[1] = winBei;
        return result;
    }
    static void main(args){
        Script script = new Script();
        Map<String, Object> zhuang = new HashMap<>();
        zhuang.put("id", 1);
        zhuang.put("num", "0.18");
        zhuang.put("account", new BigDecimal(100));
        zhuang.put("limit", new BigDecimal(100));
        List<Map<String, Object>> xianList = new ArrayList<>();
        Map<String, Object> xian1 = new HashMap<>();
        xian1.put("id", 2);
        xian1.put("num", "0.11");
        xian1.put("zhu", "20");
        xian1.put("account", new BigDecimal(200));
        Map<String, Object> xian2 = new HashMap<>();
        xian2.put("id", 3);
        xian2.put("num", "0.11");
        xian2.put("zhu", "20");
        xian2.put("account", new BigDecimal(200));
        xianList.add(xian1);
        xianList.add(xian2);
        Map result = script.settle(zhuang, xianList, new HashMap());
        result.each{ println it }
    }
}
